﻿using Dapper;
using Domain.Entity.ord;
using Domain.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repository
{
    public class Sale_dRepository : ISale_dRepository
    {

        //连接字符串
        string con = "Data Source=121.37.93.71;Initial Catalog=CSSL_ZCTWMS;User ID=sa;Password=geili@2025;Encrypt=False";

        //添加销售明细单
        public async Task<int> AddSale_d(Sale_d sale_D)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"insert into [dbo].[ord_sale_d](sale_id, proc_id, proc_name, proc_saled_num, proc_saled_price, proc_saled_total, remark)
                                values({sale_D.sale_id},{sale_D.proc_id},'{sale_D.proc_name}',{sale_D.proc_saled_num},'{sale_D.proc_saled_price}','{sale_D.proc_saled_total}','{sale_D.remark}')";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //删除销售明细单
        public async Task<int> DeleteSale_d(int saleid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"delete from [dbo].[ord_sale_d] where sale_id={saleid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        //根据销售单与货品获取到主键id
        public async Task<int> Getid(int saleid, int procid)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select saled_id from [dbo].[ord_sale_d] where sale_id={saleid} and proc_id={procid}";
                return await sqlConnection.ExecuteScalarAsync<int>(sql);
            }
        }


        //根据销售单id查询明细
        public async Task<List<Sale_d>> GetSale_ds(int saleid)
        {
            using(SqlConnection sqlConnection=new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $"select * from [dbo].[ord_sale_d] where sale_id={saleid}";
                return sqlConnection.Query<Sale_d>(sql).ToList();
            }
        }



        //更新出库数量
        public async Task<int> UpdateNum(int saledid, int oknum, int nonum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_sale_d] set saled_outstockOK_num={oknum},saled_outstockNO_num={nonum} where saled_id={saledid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }


        //更新退货数量
        public async Task<int> UpdateRNum(int saledid, int returnnum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_sale_d] set saled_returned_num={returnnum} where saled_id={saledid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<int> UpdateRNum(int saledid, int returnnum, int newnum)
        {
            using (SqlConnection sqlConnection = new SqlConnection(con))
            {
                await sqlConnection.OpenAsync();
                string sql = $@"update [dbo].[ord_sale_d] set saled_returned_num={returnnum},proc_saled_num={newnum} where saled_id={saledid}";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }
    }
}
